<template>
  <el-dialog
    title="分配权限"
    :visible="visible"
    width="50%"
    @close="closeDialog"
    @open="getPermissionList"
  >
    <el-tree
      ref="tree"
      :data="list"
      show-checkbox
      node-key="id"
      :default-expand-all="true"
      :check-strictly="true"
      :props="defaultProps"
    />
    <span slot="footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </span>
  </el-dialog>

</template>

<script>
import { getPermissionListAPI } from '@/api/permission'
import { assignPermAPI, getRoleAPI } from '@/api/settinng'
import { transTree } from '@/utils/transTree'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      userInfo: {}
    }
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionListAPI()
      this.list = transTree(res.data)
      const userInfo = await getRoleAPI(this.curId)
      // console.log(userInfo)
      this.$refs.tree.setCheckedKeys(userInfo.data.permIds)
    },
    closeDialog() {
      this.$emit('closeassign')
    },
    async confirm() {
      console.log(this.$refs.tree.getCheckedKeys())
      const res = await assignPermAPI({
        id: this.curId,
        permIds: this.$refs.tree.getCheckedKeys()
      }).catch(err => console.log(err))
      // console.log(res)
      this.$message.success(res.message)
      this.closeDialog()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
